package pessoas 
{
	import org.flixel.FlxPoint;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	/**
	 * ...
	 * @author João Brant
	 */
	public class PessoaCacadora extends Pessoa
	{
		[Embed(source = "../../assets/exclamation.png")]
		private var exclamationImg:Class;
		
		private static const CHASE_ANIMATION_DURATION:int = 15;
		protected var chaseExclamation:FlxSprite = null;
		
		protected var noticeRadius:Number = 200;
		protected var chaseMinDist:Number = 20;
		protected var mainState:MainState;
		
		protected var scaleY:Number = 1; //Usado pela mídia
		
		protected var hasChased:Boolean = false;
		protected var chasing:Boolean = false;
		protected var chaseAnimationCount:int;
		protected var chasePos:FlxPoint;
		protected var chaseVelocity:FlxPoint;
		protected var chaseSpeed:Number = 3;
		protected var keepVelocity:FlxPoint = new FlxPoint();
		
		public function PessoaCacadora() 
		{
			
		}
		
		public override function update():void
		{
			super.update();
			if (chasing && chaseAnimationCount < CHASE_ANIMATION_DURATION)
			{
				chaseAnimationCount++;
				
				this.scale.y = 1 + 0.1 * Math.cos(chaseAnimationCount);
				if (this.scale.x < 0)
				{
					this.scale.x = -1 + 0.1 * Math.sin(chaseAnimationCount);
				}
				else
				{
					this.scale.y = 1 + 0.1 * Math.sin(chaseAnimationCount);
				}
				
				if (chaseExclamation != null)
				{
					chaseExclamation.y--;
					chaseExclamation.alpha -= 1 / CHASE_ANIMATION_DURATION;
					if (chaseExclamation.alpha < 0)
					{
						chaseExclamation.destroy();
						chaseExclamation = null;
					}
				}
			}
			else
			{
				this.setScale(scale.x, scaleY);
			}
			chase();
		}
		
		protected function chase():void
		{
			var dist:Number;
			if (!chasing)
			{	
				if (!hasChased)
				{
					var playerPos:FlxPoint = new FlxPoint(mainState.salazar.x, mainState.salazar.y);
					dist = Math.sqrt((playerPos.x - x) * (playerPos.x - x) + (playerPos.y - y) * (playerPos.y - y));
					
					if (dist < noticeRadius && mainState.playerController.isSucking)
					{
						beginChaseAnimation();
						chasing = true;
						chasePos = playerPos;
						chaseVelocity = new FlxPoint(playerPos.x - x, playerPos.y - y);
						chaseVelocity.x *= chaseSpeed / dist;
						chaseVelocity.y *= chaseSpeed / dist;
						
						if (chaseVelocity.x < 0)
							setScale(-1, scale.y);
						else
							setScale(1, scale.y)
						
						keepVelocity.x = velocity.x;
						keepVelocity.y = velocity.y;
						
						velocity.x = 0;
						velocity.y = 0;
					}
				}
			}
			else
			{
				x += chaseVelocity.x;
				y += chaseVelocity.y;
				
				dist = Math.sqrt((chasePos.x - x) * (chasePos.x - x) + (chasePos.y - y) * (chasePos.y - y));
				if (dist < chaseMinDist)
				{
					chasing = false;
					hasChased = true;
					
					velocity.x = keepVelocity.x;
					velocity.y = keepVelocity.y;
					
					if (velocity.x < 0)
						setScale(-1, scale.y);
					else
						setScale(1, scale.y);
				}
			}
		}
		
		protected function beginChaseAnimation():void
		{
			chaseAnimationCount = 0;
			chaseExclamation = new FlxSprite(this.x + 8, this.y - 30, exclamationImg);
			chaseExclamation.scale.x = 0.4;
			chaseExclamation.scale.y = 0.4;
			mainState.add(chaseExclamation);
		}
		
	}

}